﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Input;

namespace Freshome.m.vms
{
    public class Magicommand : ICommand, INotifyPropertyChanged
    {
        public Magicommand(string nm, Action<object> execute)
            : this(nm, execute, null)
        {
        }
        public Magicommand(string nm, Action<object> execute, Predicate<object> doable)
        {
            if (execute == null) throw new ArgumentNullException("execute");
            else
            {
                _execute = execute;
                _doable = doable;
                _name = nm;
            }
        }
        public string Name
        {
            get
            {
                return this._name;
            }
        }
        public bool CanExecute(object parameter)
        {
            return _doable == null ? true : _doable(parameter);
        }
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
        public void Execute(object parameter)
        {
            _execute(parameter);
        }
        private readonly Action<object> _execute;
        private readonly Predicate<object> _doable;
        private readonly string _name;

        public event PropertyChangedEventHandler PropertyChanged;
        /// <summary>
        /// Raises this object's PropertyChanged event.
        /// <param name="propertyName">The property that has a new value.</param>
        /// </summary>
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
